﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using XNAGuiLib.GuiLibResourceManager;
using Microsoft.Xna.Framework.Graphics;

namespace XNAGuiLib.GuiLibTextfield
{
    public class MacTextfield : TextfieldObject
    {
        #region Protected Fields - Corner TextureObjects
        // corner top left
        TextureObject CornerTL;
        // corner top right
        TextureObject CornerTR;
        // corner bottom left
        TextureObject CornerBL;
        // corner bottom right
        TextureObject CornerBR;
        #endregion

        #region Constructor - MacTextfield(Game game, string name, string content, Vector2 relPosition, Vector2 bounds, bool scrollbarVisible)
        public MacTextfield(Game game, string name, string content, Vector2 relPosition, Vector2 bounds, bool scrollbarVisible)
            : base(game, name, content, relPosition, bounds, Color.DarkSlateGray, GuiResourceManager.ResourceManagerType.StyleMac, scrollbarVisible)
        {
            #region Init - CornerTL (topleft)
            CornerTL = new TextureObject(_rcm.Load<Texture2D>("tf_crn_tl"), "", Statics.ZeroVector2, Statics.ZeroVector2);
            CornerTL.Name = this.Name + "_corner_tl";
            CornerTL.RelativePosition = Statics.ZeroVector2;
            CornerTL.Bounds = new Vector2(CornerTL.Texture.Width, CornerTL.Texture.Height);
            #endregion

            #region Init - CornerTR (topright)
            CornerTR = new TextureObject(_rcm.Load<Texture2D>("tf_crn_tr"), "", Statics.ZeroVector2, Statics.ZeroVector2);
            CornerTR.Name = this.Name + "_corner_tr";
            if (_scrollbarVisible)
                CornerTR.RelativePosition = new Vector2(_bounds.X - CornerTR.Texture.Width - _scrollbar.Width, 0);
            else CornerTR.RelativePosition = new Vector2(_bounds.X - CornerTR.Texture.Width, 0);
            CornerTR.Bounds = new Vector2(CornerTR.Texture.Width, CornerTR.Texture.Height);
            #endregion

            #region Init - CornerBL (bottomleft)
            CornerBL = new TextureObject(_rcm.Load<Texture2D>("tf_crn_bl"), "", Statics.ZeroVector2, Statics.ZeroVector2);
            CornerBL.Name = this.Name + "_corner_bl";
            CornerBL.RelativePosition = new Vector2(0, _bounds.Y - CornerBL.Texture.Width);
            CornerBL.Bounds = new Vector2(CornerBL.Texture.Width, CornerBL.Texture.Height);
            #endregion

            #region Init - CornerBR (bottomright)
            CornerBR = new TextureObject(_rcm.Load<Texture2D>("tf_crn_br"), "", Statics.ZeroVector2, Statics.ZeroVector2);
            CornerBR.Name = this.Name + "_corner_br";
            if(_scrollbarVisible)
                CornerBR.RelativePosition = new Vector2(_bounds.X - CornerBR.Texture.Width - _scrollbar.Width, _bounds.Y - CornerBR.Texture.Width);
            else CornerBR.RelativePosition = new Vector2(_bounds.X - CornerBR.Texture.Width, _bounds.Y - CornerBR.Texture.Width);
            CornerBR.Bounds = new Vector2(CornerBR.Texture.Width, CornerBR.Texture.Height);
            #endregion

            InitializeBorderComponents();

            this._initialized = true;
        }
        #endregion


        #region Override Method - Rescale(Vector2 bounds)
        public override void Rescale(Vector2 bounds)
        {
            _bounds = bounds;
            // (re)set _keyProcessor
            _keyProcessor.LineWidth = (int)this.Bounds.X - 4;
            _lines = (int)((this.Bounds.Y + 2) / (_font.MeasureString("A").Y - 2));
            _keyProcessor.TotalLines = _lines;
            // Rescale / Reposition Scrollbar
            this._scrollbar.RelativePosition = new Vector2(((_bounds.X - _scrollbar.Width) / 2), 0);
            this._scrollbar.Rescale(new Vector2(this._scrollbar.Width, this._bounds.Y));
            // Rescale / Reposition Background
            this.TextureObject(this._textureObjs.IndexOf(this.CornerTL)).Bounds = new Vector2(CornerTL.Width, CornerTL.Height);
            this.TextureObject(this._textureObjs.IndexOf(this.CornerTL)).RelativePosition = new Vector2(0, 0);
            // Rescale / Reposition Background
            this.TextureObject(this._textureObjs.IndexOf(this.CornerTR)).Bounds = new Vector2(CornerTR.Width, CornerTR.Height);
            this.TextureObject(this._textureObjs.IndexOf(this.CornerTR)).RelativePosition = new Vector2(_bounds.X - (CornerTR.Width + _scrollbar.Width + 1), 0);
            // Rescale / Reposition Background
            this.TextureObject(this._textureObjs.IndexOf(this.CornerBL)).Bounds = new Vector2(CornerBL.Width, CornerBL.Height);
            this.TextureObject(this._textureObjs.IndexOf(this.CornerBL)).RelativePosition = new Vector2(0, _bounds.Y -(CornerBL.Height));
            // Rescale / Reposition Background
            this.TextureObject(this._textureObjs.IndexOf(this.CornerBR)).Bounds = new Vector2(CornerBR.Width, CornerBR.Height);
            this.TextureObject(this._textureObjs.IndexOf(this.CornerBR)).RelativePosition = new Vector2(_bounds.X - (CornerBR.Width + _scrollbar.Width + 1), _bounds.Y - (CornerBR.Height));
            // Rescale / Reposition Background
            this.TextureObject(this._textureObjs.IndexOf(this.Background)).Bounds = new Vector2(_bounds.X - (BorderLeft.Width + BorderRight.Width), _bounds.Y - (BorderTop.Height + BorderBottom.Height));
            this.TextureObject(this._textureObjs.IndexOf(this.Background)).RelativePosition = new Vector2(BorderLeft.Width, BorderTop.Height);
            this.TextureObject(this._textureObjs.IndexOf(this.Background)).Width -= _scrollbar.Width + 1;
            // Rescale / Reposition BorderTop
            this.TextureObject(this._textureObjs.IndexOf(this.BorderTop)).Bounds = new Vector2(_bounds.X - (BorderLeft.Width + BorderRight.Width), BorderTop.Height);
            this.TextureObject(this._textureObjs.IndexOf(this.BorderTop)).RelativePosition = new Vector2(CornerTL.Width, 0);
            this.TextureObject(this._textureObjs.IndexOf(this.BorderTop)).Width -= _scrollbar.Width + CornerTL.Width + 1;
            // Rescale / Reposition BorderBottom
            this.TextureObject(this._textureObjs.IndexOf(this.BorderBottom)).Bounds = new Vector2(_bounds.X - (BorderLeft.Width + BorderRight.Width), BorderBottom.Height);
            this.TextureObject(this._textureObjs.IndexOf(this.BorderBottom)).RelativePosition = new Vector2(_bounds.X - (CornerBR.Width), _bounds.Y);
            this.TextureObject(this._textureObjs.IndexOf(this.BorderBottom)).Width -= _scrollbar.Width + CornerBL.Width;
            this.TextureObject(this._textureObjs.IndexOf(this.BorderBottom)).X -= _scrollbar.Width + 1;
            // Rescale / Reposition BorderLeft
            this.TextureObject(this._textureObjs.IndexOf(this.BorderLeft)).Bounds = new Vector2(BorderLeft.Width, _bounds.Y - (CornerBL.Height + CornerTL.Height));
            this.TextureObject(this._textureObjs.IndexOf(this.BorderLeft)).RelativePosition = new Vector2(0, CornerBL.Height);
            // Rescale / Reposition BorderRight
            this.TextureObject(this._textureObjs.IndexOf(this.BorderRight)).Bounds = new Vector2(BorderRight.Width, _bounds.Y - (CornerTR.Height + CornerBR.Height));
            this.TextureObject(this._textureObjs.IndexOf(this.BorderRight)).RelativePosition = new Vector2(_bounds.X, _bounds.Y - (CornerBR.Height));
            this.TextureObject(this._textureObjs.IndexOf(this.BorderRight)).X -= _scrollbar.Width + 1;
        }
        #endregion

        #region Protected Method - InitializeGraphicComponents()
        protected void InitializeBorderComponents()
        {
            BorderTop.RelativePosition += new Vector2(CornerTL.Width, 0);
            BorderTop.Width -= (CornerTL.Width + CornerTR.Width);
            BorderRight.RelativePosition += new Vector2(0, -CornerTR.Height);
            BorderRight.Height -= (CornerTR.Height + CornerBR.Height);
            BorderBottom.RelativePosition += new Vector2(-CornerBR.Width, 0);
            BorderBottom.Width -= (CornerBL.Width + CornerBR.Width);
            BorderLeft.RelativePosition += new Vector2(0, CornerBL.Height);
            BorderLeft.Height -= (CornerTL.Height + CornerBL.Height);

            Background.RelativePosition = new Vector2(BorderLeft.Width, BorderTop.Height);
            Background.Bounds -= new Vector2(BorderLeft.Width, BorderTop.Height);


            Add(CornerTL);
            Add(CornerTR);
            Add(CornerBL);
            Add(CornerBR);
        }
        #endregion
    }
}
